//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package game;

import java.util.Random;

public abstract class CPU extends Player {
	
	public CPU(String name) {
		super(name);
		// TODO Auto-generated constructor stub
	}

	public abstract IAOboloHelp bestOption(Game game);

	public void cpuSelectDeckArray(Obolo[] library, String firstRule,
			int numObolo) {
		Obolo[] deck = null;
		Random random = new Random();
		int index = 0;
		int multiplicant = 1;
		int maxParameter = 0;
		Obolo auxObolo, obolo;

		deck = new Obolo[numObolo];

		if (firstRule.equals("downward")){
			multiplicant = -1;
			maxParameter=100;
		}

		for (int i = 0; i < numObolo; i++) {
			obolo = null;
			//recorro tota la llibreria buscant l'obolo més adient sengons cada cas
			while (index < library.length) {
				int parameter = 0;
				if (library[index] != null) {
					parameter += incrementParameter(library[index].getNorth());
					parameter += incrementParameter(library[index].getSouth());
					parameter += incrementParameter(library[index].getEast());
					parameter += incrementParameter(library[index].getWest());
				}
				if (firstRule.equals("downward") ){
					if (parameter <= maxParameter && parameter!=0) {
						maxParameter = parameter;
						auxObolo = obolo;
						obolo = library[index];
						library[index] = auxObolo;
						
					}
				}
				else{
					if (parameter >= maxParameter && parameter!=0) {
						maxParameter = parameter;
						auxObolo = obolo;
						obolo = library[index];
						library[index] = auxObolo;
						
					}
				}
				index++;
					
				
			}
			index = 0;
			if (firstRule.equals("downward")){
				maxParameter = 100;
			}
			else{
				maxParameter = 0;	
			}
			try {
				deck[i] = obolo.clone();
			} catch (CloneNotSupportedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		setDeck(deck);

	}

	private int incrementParameter(Skill skill) {
		if (skill instanceof SkillNumber)
			return ((SkillNumber) skill).getNumber();
		else
			return 10;
	}

	public Obolo stealObolo(Obolo[] deck) {
		int maxParameter = 0;
		Obolo bestObolo = null;
		for (int i = 0; i < deck.length; i++) {
			int parameter = 0;
			parameter += incrementParameter(deck[i].getNorth());
			parameter += incrementParameter(deck[i].getSouth());
			parameter += incrementParameter(deck[i].getEast());
			parameter += incrementParameter(deck[i].getWest());

			if (parameter > maxParameter) {
				bestObolo = deck[i];
				maxParameter = parameter;
			}
		}
		return bestObolo;
	}
}
